"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : autocommit
Case Name   : 使用alter database方式修改autocommit为off，数据库为B兼容模式，连接数据库查询设置成功
Create At   : 2023/01/16
@zou_jialiang0501162244
Description :
    1、创建两个兼容B库
    2、连接兼容B库设置当前库和另一个兼容B库autocommit=0，查询
    3、重连当前兼容B库查询autocommit，连接另一个兼容B库查询
    4、清理环境
Expect      :
    1、成功
    2、成功，查询当前B库仍为on
    3、查询均为off
    4、成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class TestAutocommit(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = ['db_autocommit_0004_1', 'db_autocommit_0004_2']

    def test_autocommit(self):
        text1 = '-----step1:创建兼容B数据库;expect:成功-----'
        self.log.info(text1)
        for dbname in self.db_name:
            create_db = f"drop database if exists {dbname};" \
                        f"create database {dbname} dbcompatibility 'B';"
            createdb_res = self.pri_sh.execut_db_sql(create_db)
            self.log.info(createdb_res)
            self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, createdb_res,
                          f"创建兼容B库失败")

        text2 = '-----step2:连接兼容B库设置当前库和另一个兼容B库' \
                'autocommit=0，查询;expect:成功，查询当前B库仍为on-----'
        self.log.info(text2)
        sql = f'alter database {self.db_name[0]} set autocommit = 0;' \
              f'alter database {self.db_name[1]} set autocommit to off;' \
              f'show autocommit;'
        sql_res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name[0])
        self.log.info(sql_res)
        self.assertEqual(
            sql_res.count(self.constant.ALTER_DATABASE_SUCCESS_MSG),
            2, f'设置autocommit失败: {text2}')
        self.assertEqual(sql_res.splitlines()[-2].strip(), 'on',
                         f'执行失败: {text2}')

        text3 = '-----step3:重连当前兼容B库、连接另一个兼容B库，查询' \
                'autocommit;expect:查询均为off-----'
        self.log.info(text3)
        for dbname in self.db_name:
            show_res = self.pri_sh.execut_db_sql('show autocommit;',
                                                 dbname=dbname)
            self.log.info(show_res)
            self.assertEqual(show_res.splitlines()[-2].strip(), 'off',
                             f'执行失败: {text3}')

    def tearDown(self):
        text4 = '-----step4:清理环境; expect:成功-----'
        self.log.info(text4)
        drop_db_res = self.pri_sh.execut_db_sql(
            f"drop database if exists {self.db_name[0]};"
            f"drop database if exists {self.db_name[1]};")
        self.log.info(drop_db_res)
        self.assertEqual(drop_db_res.count(self.constant.DROP_DATABASE_SUCCESS),
                         2, f'删库失败: {text4}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
